package main

import (
	"bufio"
	"fmt"
	"os"
	"regexp"
	"strconv"
	"strings"
)

// 定义结构体存储解析后的数据
type NpuInfo1 struct {
	NpuID         int
	ChipID        int
	DeviceID      int
	BusID         string
	Health        string
	Power         float64
	Temperature   int
	AICoreUsage   int
	MemoryUsageMB int
	TotalMemoryMB int
}

func main2() {
	// 调用 npu-smi info 命令
	//output, err := exec.Command("npu-smi", "info").Output()
	//if err != nil {
	//	fmt.Println("执行 npu-smi info 命令时出错：", err)
	//	return
	//}

	// 读取demo2.txt的内容
	fileContent, err := os.ReadFile("./gpu_demo/demo2.txt")
	if err != nil {
		fmt.Println("读取文件时出错：", err)
		return
	}

	// 解析输出
	npuInfos, err := parseNpuSmiOutput(fileContent)
	if err != nil {
		fmt.Println("解析输出时出错：", err)
		return
	}

	// 打印解析结果
	fmt.Println("解析结果：")
	for _, info := range npuInfos {
		fmt.Printf("NPU ID: %d\n", info.NpuID)
		fmt.Printf("Chip ID: %d\n", info.ChipID)
		fmt.Printf("Device ID: %d\n", info.DeviceID)
		fmt.Printf("Bus ID: %s\n", info.BusID)
		fmt.Printf("Health: %s\n", info.Health)
		fmt.Printf("Power (W): %.2f\n", info.Power)
		fmt.Printf("Temperature (C): %d\n", info.Temperature)
		fmt.Printf("AI Core Usage (%): %d\n", info.AICoreUsage)
		fmt.Printf("Memory Usage (MB): %d / %d\n", info.MemoryUsageMB, info.TotalMemoryMB)
		fmt.Println("--------------------------------------------------")
	}
}

func parseNpuSmiOutput(output []byte) ([]NpuInfo1, error) {
	// 使用 bufio 按行读取输出
	scanner := bufio.NewScanner(strings.NewReader(string(output)))
	var npuInfos []NpuInfo1
	var currentInfo NpuInfo1

	// 定义正则表达式匹配关键信息
	re := regexp.MustCompile(`\| ([0-9]+)\s+([0-9]+)\s+\| ([A-Z]+)\s+\| ([0-9.]+)\s+\|\s+([0-9]+)\s+\|\s+([0-9]+)\s+\/\s+([0-9]+)\s+\|`)
	re2 := regexp.MustCompile(`\| ([0-9]+)\s+([0-9]+)\s+\| ([A-Z]+)\s+\| ([0-9.]+)\s+\|\s+([0-9]+)\s+\|\s+([0-9]+)\s+\/\s+([0-9]+)\s+\|\s+([0-9]+)\s+\/\s+([0-9]+)\s+\|`)

	for scanner.Scan() {
		line := scanner.Text()
		if matches := re.FindStringSubmatch(line); len(matches) > 0 {
			currentInfo.NpuID, _ = strconv.Atoi(matches[1])
			currentInfo.ChipID, _ = strconv.Atoi(matches[2])
			currentInfo.Health = matches[3]
			currentInfo.Power, _ = strconv.ParseFloat(matches[4], 64)
			currentInfo.Temperature, _ = strconv.Atoi(matches[5])
			currentInfo.MemoryUsageMB, _ = strconv.Atoi(matches[6])
			currentInfo.TotalMemoryMB, _ = strconv.Atoi(matches[7])
			npuInfos = append(npuInfos, currentInfo)
		} else if matches := re2.FindStringSubmatch(line); len(matches) > 0 {
			currentInfo.NpuID, _ = strconv.Atoi(matches[1])
			currentInfo.ChipID, _ = strconv.Atoi(matches[2])
			currentInfo.Health = matches[3]
			currentInfo.Power, _ = strconv.ParseFloat(matches[4], 64)
			currentInfo.Temperature, _ = strconv.Atoi(matches[5])
			currentInfo.AICoreUsage, _ = strconv.Atoi(matches[6])
			currentInfo.MemoryUsageMB, _ = strconv.Atoi(matches[7])
			currentInfo.TotalMemoryMB, _ = strconv.Atoi(matches[8])
			npuInfos = append(npuInfos, currentInfo)
		}
	}

	if err := scanner.Err(); err != nil {
		return nil, err
	}

	return npuInfos, nil
}
